package reflectionV2.cglib;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

/**
 * @author ChangLiang
 * @date 2020/3/18
 */
public class UserDaoProxy implements MethodInterceptor {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {

        Object result;

        if (StringUtils.equals(method.getName(), "save")) {
            logger.info("begin transaction");
            result = proxy.invokeSuper(obj, args);
            logger.info("end transaction");
        } else {
            result = proxy.invokeSuper(obj, args);
        }
        return result;
    }
}
